package com.xin.chaingeneration.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xin.chaingeneration.common.ResponseEntity;
import com.xin.chaingeneration.entity.News;
import com.xin.chaingeneration.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    NewsService newsService;

    @PostMapping("/updateNews")
    public ResponseEntity<String> updateNews(@RequestBody List<News> news){

        newsService.updateBatchById(news);

        return ResponseEntity.success("保存成功");
    }

    @PostMapping("/saveNews")
    public ResponseEntity<String> saveNews(@RequestBody List<News> news){

        List<News> newsNotNull = news.stream()
                .filter(item -> item.getContent().length() >= 10)
                .collect(Collectors.toList());

        newsService.saveBatch(newsNotNull);

        return ResponseEntity.success("保存成功");
    }

    @GetMapping("/getNewsByProductId/{productId}")
    public ResponseEntity<List<News>> getNewsByProductId(@PathVariable Long productId){
        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(News::getProductId,productId);

        List<News> list = newsService.list(queryWrapper);

        ResponseEntity<List<News>> responseEntity = ResponseEntity.success(list);
        responseEntity.setCode(1);

        return responseEntity;
    }

    @PostMapping("/deleteAll")
    public ResponseEntity<String> deleteAll(){

        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(News::getProductId,1L);

        newsService.remove(queryWrapper);

        return ResponseEntity.success("deleted");
    }

    @PostMapping("/deleteEmpty")
    public ResponseEntity<String> deleteEmpty(){

        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.apply("LENGTH(content) < {0}", 3);

        newsService.remove(queryWrapper);

        return ResponseEntity.success("deleted");
    }

    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<String> deleteById(@PathVariable Long id ){

        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(News::getProductId,id);

        newsService.remove(queryWrapper);

        return ResponseEntity.success("deleted");
    }

    @PostMapping("/allNews")
    public ResponseEntity<List<News>> getAllNews(){
        List<News> list = newsService.list();
        List<News> result = new ArrayList<>();
        for (News news : list) {
            if (news.getContent().length()>=10){
                result.add(news);
            }
        }

        ResponseEntity<List<News>> responseEntity = ResponseEntity.success(result);

        return responseEntity;
    }


}
